package org.dragonnova.iot.business.cloud.transport.dao;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.List;

import org.dragonnova.iot.business.cloud.transport.model.THTagValue;
import org.springframework.jdbc.core.ParameterizedPreparedStatementSetter;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Transactional
@Repository
public class TagJdbcDao extends JdbcDaoSupport {

	private static String insert_batch_t_tag_value = "INSERT INTO t_tag_value"
			+ "(tag_sn,tag_type,temp,hum,battery,updatetime,inserttime) "
			+ "VALUES (?,?,?,?,?,?,?)";


	public int[][] batchInsertTags(List<THTagValue> values) {
		int[][] updateCounts = this.getJdbcTemplate().batchUpdate(
				insert_batch_t_tag_value, values, 100,
				new ParameterizedPreparedStatementSetter<THTagValue>() {

					@Override
					public void setValues(PreparedStatement ps, THTagValue value)
							throws SQLException {
						ps.setString(1, value.getIdentifier());
						ps.setInt(2, value.getTagType());
						ps.setFloat(3, value.getTemplatureNum());
						ps.setFloat(4, value.getHumidityNum());
						ps.setFloat(5, value.getBattery());
						ps.setTimestamp(6, new Timestamp(value.getUpdateTime()
								.getTime()));
						ps.setTimestamp(7, new Timestamp(value.getInsertTime()
								.getTime()));
					}
				});
		return updateCounts;
	}
}
